<?php
/**
 * Created by PhpStorm.
 * User: sunbo
 * Date: 2019/7/19
 * Time: 11:41 AM
 */

namespace app\index\controller;
use think\Exception;
include_once 'sdk_utils/Utils.php';
include_once 'sdk_utils/RsaEncrypt.php';

class CitPay_Http_API extends CoinBaseContorller{
    public $merchant_id = "";
   // public $public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjH5NjwTIRxvr3QjqQzrYXqiC+86N19iqc/yNDvX+KXnuqO04D8PDEi8N1g0WQeTovqMIh7bEoGqA+0/x7EtYT6e1clx0P/hIJXws7b6PIA1mCfXdqINqOcXdUftsZf1prynLz1AuOzbQw6z3CtcTmdS7r9cCf3nlN+icj6wO4RYaQIAbfmrkk/G5HGJxWnljFM9SplObuBHjp1H9eIpEXDFmar0mqlq2dHCH7kBrZDIms1aBfhR4acFoDEAeJI9GLulnQb42ij45ewkOQDkI+g78gxkmJ6dl6h4rIRtt9S9FAfdPXvBoSj0Dec/f6oszSyCEf7vNErMpErZc52U9eQIDAQAB";
    public $public_key="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqGnQbM+RviQXBhX0U2znbCNAYx98+4mRYF/n2z/7cNf/zYWT53vTHHv+aKkH2l96ZoL3K3n+G0FBfumhIHwDD6zMC7to447GkHpNX21YfUxSfx/fRhFyX3ty8FhPpHKFi9vqR5CWJRnGr41NSRzbrLeirf1NIi1B8KXxScLkdSRNHjzJ7Z8Kd2frMVD2HYBLZILSMFYIcte7G7bQP4akBCozebeWWFeoZqHLM5K0RT4LxupwjktTjFbwnA/juToSmrgXC9RuU83EAoyR28OtMhe8JZC8pN/hSdT4s5RUDwPowaxjCgYSKphTata8MxqtHRIT+1LFVfZQLBlbA2LHrQIDAQAB";
    public $requestUrl ="http://54.183.116.40:8080/openapi/v2/";// "https://vip.citpay.com/openapi/v2/";
    public $notify_url =  "";//"http://54.153.14.185/bc/notify.php"; //回调地址[待替换]
    public $pri_pem_name="";
    // 商户AES加密秘钥， citpay商户开通后，在系统中获取
    public $APP_SECRT_CODE = "";
    function __construct() {
        parent::__construct();
        $row= $this->db_select("select  name ,value from token_sys_config where name in ('APP_SECRT_CODE','CALL_BACK_URL','APP_KEY','PRI_PEM_NAME')");
        for($x=0;$x<count($row);$x++){
            switch ($row[$x]["name"]){
                case "APP_SECRT_CODE":
                    $this->APP_SECRT_CODE=$row[$x]["value"];
                    break;
                case "CALL_BACK_URL":
                    $this->notify_url=$row[$x]["value"];
                    break;
                case "APP_KEY":
                    $this->merchant_id = $row[$x]["value"];
                    break;
                case "PRI_PEM_NAME":
                    $this->pri_pem_name = $row[$x]["value"];
                    break;
            }
        }
    }

    public function http_request($array,$method) {
        $this->log("CITPay接口和数据:".$method." data:".json_encode($array));
        //请求参数体 body
        $content =  getSignContent($array);
        // 请求参数AES加密
        $abc = encrypt( $content  ,$this->APP_SECRT_CODE);
        //var_dump($abc);
        $requestParams = array(
            // 在citpay平台中获取
            "client_key" => $this->merchant_id,
            "request_content" => $abc ,
            "sign_type" => "RSA2",
            "sign"=>"",
        );
        $rsa = new sdk_utils\RsaEncrypt();
        // 必须赋值：citpay公钥 从平台获取
        $rsa->citpayPublicKey = $this->public_key;
        // 指定第三方平台的私钥文件路径
        $rsa->mallRsaPrivateKeyFilePath = APP_PATH."PrivateKeys/".$this->pri_pem_name;
        $signStr = $rsa->rsaSign($requestParams);

        $requestParams['sign'] = $signStr;
        //var_dump($requestParams);
        // URL : httprequest请求url
        $response = curl($this->requestUrl.$method ,$requestParams);
        $response = json_decode($response, true);
        if($response["code"] != 200&&$method!="accountcheck"){
            exception("CitPay错误:[" . $method . "]" . $response['message']." data:".json_encode($array));
            return;
        }
        //验签返回结果是否正确
        $res =  $rsa->rsaCheckV1($response , "");
        if($res == true) { //8987654
                    return $response;
        }else{
            exception ("CitPay错误:[".$method."]验签失败");
            return ;
        }
    }

    public function rsaCheckV1($data) {
        $rsa = new sdk_utils\RsaEncrypt();
        // 必须赋值：citpay公钥 从平台获取
        $rsa->citpayPublicKey = $this->public_key;
        // 指定第三方平台的私钥文件路径
        $rsa->mallRsaPrivateKeyFilePath = APP_PATH."PrivateKeys/".$this->pri_pem_name;
        return ($rsa->rsaCheckCallback($data,$rsa->citpayPublicKey));
    }
    public function decrypt($data) {
        $content = decrypt($data,$this->APP_SECRT_CODE);
        parse_str($content,$arr);
        return $arr;
    }
    public function signStr($requestParams) {
        $rsa = new sdk_utils\RsaEncrypt();
        // 必须赋值：citpay公钥 从平台获取
        $rsa->citpayPublicKey = $this->public_key;
        // 指定第三方平台的私钥文件路径
        $rsa->mallRsaPrivateKeyFilePath = APP_PATH."PrivateKeys/".$this->pri_pem_name;
        $signStr = $rsa->rsaSign($requestParams);
        return $signStr;
    }
}